package ph.com.gs3.formalistics.view.document.specialfields;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.model.vo.business.form.FormFieldData;
import ph.com.gs3.formalistics.view.document.abstractfields.TextTypeFieldView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class PickListField extends TextTypeFieldView {

	public static final String TAG = PickListField.class.getSimpleName();

	private TextView tvLabel;

	private PickListFieldListener listener;

	public PickListField(Context context, FormFieldData formField) {
		super(context, formField);

		createView();

		setLabel(formField.getLabel());
		setPlaceholder(formField.getPlaceHolder());
		setValue(formField.getValue());

	}

	protected void createView() {
		LayoutInflater.from(getContext()).inflate(R.layout.field_pick_list, this, true);

		tvLabel = (TextView) findViewById(R.id.Picklist_tvLabel);
		setEditTextValueField((EditText) findViewById(R.id.Picklist_etResultText));
		getEditTextValueField().setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showPickerView();
			}
		});

		this.setTag(fieldId + "_container");
		getEditTextValueField().setTag(fieldId);
	}

	private void showPickerView() {

		if (listener != null) {
			listener.onOpenPicklistCommand(this);
		}

	}

	// =====================================================================
	// {{ Getters & Setters

	public void setLabel(String label) {
		tvLabel.setText(label);
	}

	public void setPlaceholder(String placeholder) {
		if (placeholder != null && !placeholder.isEmpty()) {
			getEditTextValueField().setHint(placeholder);
		} else {
			// Default Placeholder
			getEditTextValueField().setHint("Tap to pick");
		}
	}

	public PickListFieldListener getListener() {
		return listener;
	}

	public void setListener(PickListFieldListener listener) {
		this.listener = listener;
	}

	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		getEditTextValueField().setEnabled(enabled);
		getEditTextValueField().setClickable(enabled);
	}

	public FormFieldData getSourceFormField() {
		return formField;
	}

	// }}

	public static interface PickListFieldListener {

		public void onOpenPicklistCommand(PickListField source);

	}

	@Override
	public String getValue() {
		return getEditTextValueField().getText().toString();
	}

	@Override
	public void setValue(String value) {
		getEditTextValueField().setText(value);
	}

}
